{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f7ba29e0",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/10/24/week 10\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c223af9",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "\n",
    "> 1.while循环\n",
    "\n",
    "> 2.购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b974bea4",
   "metadata": {},
   "source": [
    "## 1.while循环\n",
    "\n",
    "* 回顾： for 循环； for + range() 可以指定循环的次数 --> 关于网页URL队列的抓取实践\n",
    "\n",
    "* while循环：当某种条件成立时，停止循环\n",
    "\n",
    "* while True：无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "55d387f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯~ 0\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯~ 9\n"
     ]
    }
   ],
   "source": [
    "# 1.指定次数\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯~\",count)\n",
    "    count = count + 1\n",
    "    if count == 10:\n",
    "        break  # break 终止循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8bd9e824",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：admin\n",
      "密码：123456\n",
      "登录成功，欢迎~admin\n"
     ]
    }
   ],
   "source": [
    "# 2.while True循环，可以让用户重复输入\n",
    "user01 = \"admin\"\n",
    "passward01 = \"123456\"\n",
    "count = 0 #计数器\n",
    "\n",
    "while True:\n",
    "    username = input(\"账号：\")\n",
    "    passward = input(\"密码：\")\n",
    "    \n",
    "     # 情景一：账号密码都正确\n",
    "    if user01 == username and passward01 == passward:\n",
    "        print(\"登录成功，欢迎~\"+user01)\n",
    "        break # 中止循环\n",
    "        \n",
    "     # 情景二：账号不存在或密码不正确，用户有三次机会，输入三次之后，1小时之后再来尝试\n",
    "    else:\n",
    "        if count<2:\n",
    "            print(\"账号不存在或密码不正确!请重新输入！您还有\"+str(2-count)+\"次尝试的机会\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确！请1个小时后再来尝试\")\n",
    "            break\n",
    "        count += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34b1d388",
   "metadata": {},
   "source": [
    "## 2.购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "68cef60a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e777de1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：50\n",
      "--------商品列表---------\n",
      "商品序号: 0 蜜雪柠檬水 6元\n",
      "商品序号: 1 蜜桃四季春 10元\n",
      "商品序号: 2 芒果奶盖 12元\n",
      "商品序号: 3 葡萄啵啵奶茶 8元\n",
      "请输入商品编号:2\n",
      "商品购买成功，正在制作中...您的余额还有38元\n",
      "--------商品列表---------\n",
      "商品序号: 0 蜜雪柠檬水 6元\n",
      "商品序号: 1 蜜桃四季春 10元\n",
      "商品序号: 2 芒果奶盖 12元\n",
      "商品序号: 3 葡萄啵啵奶茶 8元\n",
      "请输入商品编号:q\n",
      "您已购买的商品： [12]\n",
      "您的余额还有38,欢迎下次光临~\n"
     ]
    }
   ],
   "source": [
    "# 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "goods_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "   [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "\n",
    "shopping_car = []\n",
    "\n",
    "AliPay = int(input(\"请输入余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"--------商品列表---------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号:\",i,goods_list[i][0],str(goods_list[i][1])+\"元\")   # 格式化输出\n",
    "\n",
    "    # 2.让用户输入商品编号进行商品的购买;直接用商品列表的 index 作为商品标号\n",
    "    user_choice = input(\"请输入商品编号:\")\n",
    "    \n",
    "    # 情景1：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if AliPay >= goods_list[int(user_choice)][1]:\n",
    "            AliPay = AliPay - goods_list[int(user_choice)][1]\n",
    "            shopping_car.append(goods_list[int(user_choice)][1])\n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(AliPay))\n",
    "        # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~\")\n",
    "    # 情景2：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品：\",shopping_car)\n",
    "        print(\"您的余额还有%s,欢迎下次光临~\"%(AliPay))\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88a7b0f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设用户选择了0\n",
    "goods_list[0][1]\n",
    "# 用户选择的商品价格： goods_list[user_choice][1]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
